package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.RandomUtil;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.codemodel.internal.JForEach;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author zhaolie
 * @since 2021-01-04
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        Integer count = ucenterClient.countRegister(day);
        System.out.println("count+++++>" + count);
        QueryWrapper<StatisticsDaily> dailyQueryWrapper = new QueryWrapper<>();
        dailyQueryWrapper.eq("date_calculated", day);
        StatisticsDaily daily = baseMapper.selectOne(dailyQueryWrapper);
        if (daily != null) {
            daily.setRegisterNum(count);
            daily.setDateCalculated(day);
            daily.setVideoViewNum(RandomUtils.nextInt(100, 200));
            daily.setLoginNum(RandomUtils.nextInt(100, 200));
            daily.setCourseNum(RandomUtils.nextInt(100, 200));
            baseMapper.updateById(daily);
        } else {
            StatisticsDaily daily1 = new StatisticsDaily();
            daily1.setRegisterNum(count);
            daily1.setDateCalculated(day);
            daily1.setVideoViewNum(RandomUtils.nextInt(100, 200));
            daily1.setLoginNum(RandomUtils.nextInt(100, 200));
            daily1.setCourseNum(RandomUtils.nextInt(100, 200));
            baseMapper.insert(daily1);
        }
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("date_calculated", type);
        queryWrapper.between("date_calculated", begin, end);
        List<StatisticsDaily> dailyList = baseMapper.selectList(queryWrapper);
        List<String> dateList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        for (StatisticsDaily daily : dailyList) {
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", dateList);
        map.put("numDataList", numDataList);
        return map;
    }
}
